<template>
  <div class="ref-goods" v-for="item in oneCategory?.children" :key="item.id">
    <div class="head">
      <h3>- {{ item.name }} -</h3>
      <p class="tag">温暖柔软，品质之选</p>
      <XtxMore />
    </div>
    <div class="body">
      <GoodsItem :goods="goods" v-for="goods in item.goods" :key="goods.id" />
    </div>
  </div>
</template>

<script>
import GoodsItem from "@/views/category/components/GoodsItem";
import { getTopCategoryById } from "@/api/category";
import { onBeforeRouteUpdate, useRoute } from "vue-router";
import { ref } from "vue";
export default {
  name: "CommandGoods",
  components: { GoodsItem },
  setup() {
    // 获取路由实例i
    const route = useRoute();
    const { oneCategory, getData } = useCategory();
    getData(route.params.id);
    return {
      oneCategory,
    };
  },
};
function useCategory() {
  // 定义变量存储一级分类类目
  const oneCategory = ref();

  const getData = (id) => {
    getTopCategoryById(id).then((data) => {
      // 保存数据
      oneCategory.value = data.result;
    });
  };
  // 路由切换时触发;
  onBeforeRouteUpdate((to) => getData(to.params.id));
  return {
    oneCategory,
    getData,
  };
}
</script>
<style lang="less" scoped>
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>
